$(document).ready(function() {
    editNews.init();
});

var editNews = (function() {
    function editNews(){
        var title = $('#newsTitle').val();
        
        $('.form-group').removeClass('has-error');
        $('.errorHandler').html('');
        if (!title) {
            $('#newsTitle').parents('.form-group').addClass('has-error');
            $('#editNews').find('i.fa').attr('class', 'fa fa-plus-circle');
            return false;
        }
        
        var lstProfile = "";
        for(var i in $("#demo3").tagit("tags"))
            lstProfile += $("#demo3").tagit("tags")[i].value + ",";
        $('#demo3Out').html('<input type="hidden" id="postListProfile" name="postListProfile" value="' + lstProfile + '" />');
        
        $('#newsContent').val(tinyMCE.activeEditor.getContent());
        $('#appInput').ajaxSubmit({
            url: '/crawler/ajax-edit',
            type: 'post',
            dataType: 'json',
            timeout: 3000,
            success: function(resp) { 
                if (resp.err === 0) {      //chuyen trang              
                    //$('#editNews').find('i').attr('class','fa fa-save');
                    //$('.errorHandler').html('<small><small><strong class="text-success">' + resp.msg + '</strong></small></small>').show();
                    //$('#newsImage').val('');
                    //callback();
                    //window.location.href = '/news/edit?id=' + resp.data.id + '#msg=' + resp.msg;
                    window.location.href= '/crawler/list';
                } 
                else {
                    $('.errorHandler').html('<small><small><strong class="text-danger">' + resp.msg + '</strong></small></small>').show();
                }
            }
        });
    }
    
    function deleteNews(id, title, callback){
        showConfirm({
            msg: 'Bạn có chắc chắn muốn xoá tin này không? <strong class="text-danger">' + title +'</strong>',
            title: 'Xoá Tin tức',
            noBt: 'Đóng',
            yesBt: 'Đồng ý xoá',
            onYes: function() { 
                $.post('/crawler/ajax-delete', {id: id}, function(resp) {
                    if(resp.err === 0){
                        callback();
                        hideConfirm();
                    }else {
                         hideConfirm();
                        $('.errorHandler').removeClass('text-success').addClass('text-danger').html(resp.msg).show();
                    }
                }, 'json');
            }
        });
    }
    
    function initHanlder(){
        initWysiwyg();

        $('#editNews').click(function() {
            $(this).find('i.fa').attr('class', 'fa fa-spinner fa-spin');
            editNews();
            return false;
        });
        
        $('#deleteNews').click(function() {
            var id = $('#newsId').val(),
                title = $('#newsTitle').val();
            deleteNews(id, title, function() {
                window.location.href='/crawler';
            });
            return false;
        });

        var hash = window.location.hash;
        if(hash.indexOf('#msg=') > -1){
            $('.errorHandler').html(hash.substring(5)).show();
            window.location.hash = '';
        }
    }
    return {
        init: function() {
            initHanlder();
            initDatePicker('#newsTime');
        },
        
        deleteNews: function(newsId, title, obj){
            deleteNews(newsId, title, function(){
                $(obj).remove();
            });
            return false;
        }
    };
})();